package com.zx._07_数据库.测试数据;

import com.zx._07_数据库.jdbc.JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SaveClass {


    public static void save() {
        log.info("save class begin");
        String sql = "INSERT INTO t_class (id, grade, clazz) VALUES(?, ?, ?)";

        Connection connection = JDBC.getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            int id = 1;
            int grade = 1;
            int clazz = 1;
            int count = Config.STUDENT_COUNT;
            while (true) {
                ps.setInt(1, id);
                ps.setInt(2, grade);
                ps.setInt(3, clazz);
                ps.addBatch();
                id++;
                clazz++;

                count -= Config.CLASS_STUDENT_COUNT;
                if (count <= 0) {
                    break;
                }

                if (clazz > Config.GRADE_CLASS_LIMIT) {
                    grade++;
                    clazz = 1;
                }
            }

            ps.executeBatch();
            log.info("save class finsh");
        } catch (Exception e) {
            log.error("save class error", e);
        } finally {
            JDBC.release(connection, ps, null);
        }
    }
}
